package com.hmall.order.interceptor;

import com.hmall.order.pojo.IDThreadLocal;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("authorization");
        if (StringUtils.isBlank(authorization)){
            response.setStatus(403);
            return false;
        }
        Long id = Long.valueOf(authorization);
        IDThreadLocal.setId(id);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        IDThreadLocal.remove();
    }
}
